<template>
  <a-row type="flex" style="flex-flow: inherit;">
    <a-col :flex="toggle ? '260px' : '30px'" style="margin-right: 12px;background-color: #fff;">
      <depart-tree :rootOpened="true" :multi="false" @ok="getTreeData" @toggleBool="toggleBool" />
      <!-- <groups-tree @tree="getTreeData" @selectedKeys="getSelectedKeys" /> -->
    </a-col>
    <a-col flex="auto">
      <a-card :bordered="false" style="margin-bottom: 10px">
        <!-- 查询区域 -->
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper" style="position: relative">
          <div class="search-wrapper-btn">
            <a-button type="link" @click="show = !show">
              <span v-if="!show">展开
                <a-icon type="down" />
              </span>
              <span v-else>收起
                <a-icon type="up" />
              </span>
            </a-button>
          </div>
          <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="onQuery">
            <div class="mode_item">
              <div class="mode_item_div">
                <a-form-model-item label="人员名称">
                  <a-input v-model="pageFilter.realName" placeholder="请输入" />
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="人员类型">
                  <a-select v-model="pageFilter.personType" allowClear placeholder="请选择" style="width:100%;">
                    <a-select-option value="">全部</a-select-option>
                    <a-select-option v-for="(item, key) in typeArr" :key="key" :value="item.value">{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="身份证号">
                  <a-input v-model="pageFilter.identityCard" placeholder="请输入" />
                </a-form-model-item>
              </div>
            </div>
            <div class="mode_item" v-show="show">
              <div class="mode_item_div mode_item_flex">
                <a-form-model-item label="最新回访时间">
                  <a-range-picker v-model="rvTime" allowClear style="width:100%;" show-time format="YYYY-MM-DD HH:mm"
                    valueFormat="YYYY-MM-DD HH:mm" @change="onTimeChange" />
                </a-form-model-item>
              </div>
            </div>
            <div class="btnBox">
              <a-form-model-item :wrapper-col="{ offset: 0 }" style="margin-bottom: 0">
                <a-button type="primary" @click="onQuery"> 查询 </a-button>
                <a-button style="margin-left: 10px" @click="onResetForm"> 重置 </a-button>
              </a-form-model-item>
            </div>
          </a-form-model>
        </div>
      </a-card>
      <a-card :bordered="false">
        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="onOut()">导出</a-button>
        </div>
        <!-- 表格展示 -->
        <div class="table-page-content">
          <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="false" :loading="loading"
            :columns="columns" :dataSource="pageData" @change="onTableChange">
            <template slot="personType" slot-scope="text">
              <span>{{ setTittle(text) }}</span>
              <!-- <span>{{text}}</span> -->
              <!-- <span>{{record}}</span> -->
            </template>

            <template slot="action" slot-scope="text, record">
              <a-button @click="onAdd(record)" type="link" style="padding: 0">添加回访</a-button>
              <a-divider type="vertical" />
              <a-button @click="onHistory(record)" type="link" style="padding: 0">历史记录</a-button>
            </template>
          </z-table>
        </div>
      </a-card>
    </a-col>
    <AddModal ref="addModalId" @ok="onQuery" />
  </a-row>
</template>

<script>
import { getDictItemsFromCache } from '@/api/api'
import { downLoadFileReader } from '@/utils/export-util/util'
import * as returnVisitApi from '@/api/sjsk/help/returnVisit.js'
import AddModal from './modules/AddModal'
import DepartTree from '@/views/sjsk/modules/departTree'
export default {
  components: {
    DepartTree, AddModal
  },
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      show: false,
      loading: false,
      visible: false,
      reloading: false,
      rvTime: [],
      treeDatas: [],
      toggle: true,
      typeArr: getDictItemsFromCache('personType'),
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '姓名',
          dataIndex: 'realName',
          align: 'center',
        },
        {
          title: '身份证',
          dataIndex: 'identityCard',
          align: 'center',
        },
        {
          title: '人员类型',
          align: 'center',
          dataIndex: 'personType',
          scopedSlots: { customRender: 'personType' },
        },
        {
          title: '结案日期',
          dataIndex: 'closureDate',
          align: 'center',
        },
        {
          title: '最新回访时间',
          dataIndex: 'lastRvTime',
          align: 'center',
          scopedSlots: { customRender: 'lastRvTime' },
        },
        {
          title: '操作',
          width: 150,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {},
      departmentId: ""
    }
  },
  created() { },
  async mounted() {
    // this.onQuery()
  },
  // watch: {
  //   // 组织机构树-联动筛选人员列表
  //   'pageFilter.departmentId'(newVal, oldVal) {
  //     this.onQuery()
  //   }
  // },
  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10,
        departmentId: this.departmentId
      }
      this.rvTime = []
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      returnVisitApi.page(this.pageFilter).then((res) => {
        if (res.success) {
          this.pageData = res.result ? res.result : {}
          console.log(this.pageData);
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    onTimeChange(date) {
      this.$set(this.pageFilter, 'startRvDate', date[0])
      this.$set(this.pageFilter, 'endRvDate', date[1])
    },
    setTittle(text) {
      return this.$filterDictTextByCache('personType', text)  //现在字典中对应的文字，是全局函数
    },
    onAdd(record) {
      this.$refs.addModalId.show(record, this.departmentId)
    },
    onHistory(record) {
      this.$router.push({
        path: '/returnVisit/record',
        query: {
          ceId: record.ceId,
          sopId: record.sopId,
          spId: record.spId
        }
      })
    },
    onOut() {
      returnVisitApi.exportRvRecord(this.pageFilter).then(async res => {
        // console.log(res);
        const fileName = '回访记录.xls'
        await downLoadFileReader(res, fileName)
      })
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = true
      this.$nextTick(() => (this.reloading = false))
    },
    // 选中组织架构树，赋值到人员列表搜索框
    getTreeData(datas, id) {
      if (id != undefined && id != null && id != "") {
        this.departmentId = id
        this.$set(this.pageFilter, 'departmentId', id)
        this.onQuery()
      }
    },
    toggleBool(bool) {
      console.log(bool)
      this.toggle = bool
    }
  },
}
</script>
<style lang="less" scoped>
.mode_item {
  display: flex;
  border-bottom: 1px #dadada dashed;

  .mode_item_div {
    width: 26%;
    padding: 5px 0;

    .ant-form-item {
      margin-bottom: 0;
    }
  }

  &>* {
    width: 100%;
    margin-bottom: 0;
    padding: 10px 0;
  }
}

.mode_item_flex {
  width: 50% !important;

  /deep/ .ant-col-6 {
    width: 19% !important;
  }
}

.search-wrapper-btn {
  position: absolute;
  right: 15%;
  top: 9px;
  z-index: 100;
}

.btnBox {
  position: absolute;
  right: 0;
  top: 5px;
  z-index: 120;
}

@import '~@assets/less/common.less';
</style>
